<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: tagcloud.php 140 2013-09-22 07:44:23Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 140 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:44:23 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$box_smarty = new smarty;

if(!CacheCheck() && !FORCE_CACHE)
	$box_smarty->caching = false;
else {
	$box_smarty->caching = true;
	$box_smarty->cache_lifetime=CACHE_LIFETIME;
	$box_smarty->cache_modified_check=CACHE_CHECK;
	$cache_id = $_SESSION['language'].$_SESSION['tag_box'];
}

if(!$box_smarty->isCached(CURRENT_TEMPLATE.'/boxes/box_tagcloud.html', $cache_id) || !$cache){
	function kshuffle(&$array) {
	    if(!is_array($array) || empty($array))
	        return false;
	    $tmp = array();
	    foreach($array as $key => $value)
	        $tmp[] = array('k' => $key, 'v' => $value);

	    shuffle($tmp);
	    $array = array();
	    foreach($tmp as $entry)
	        $array[$entry['k']] = $entry['v'];
	    return true;
	}

	function printTagCloud($tags) {
		
		$h_tag = array(	array(0 => '<h3>', 1 => '</h3>'),
	    				array(0 => '<h4>', 1 => '</h4>'),
	    				array(0 => '<h5>', 1 => '</h5>'),
	    				array(0 => '<h6>', 1 => '</h6>'));
		
		kshuffle($tags);

	    $max_size = 32;
	    $min_size = 12;

	    $max_qty = max(array_values($tags));
	    $min_qty = min(array_values($tags));

	    $spread = $max_qty - $min_qty;
	    if($spread == 0)
	        $spread = 1;

	    $step = ($max_size - $min_size) / ($spread);

	    foreach ($tags as $key => $value) {
	    	$h_rand = rand(0, 3);
			$size = round($min_size + (($value - $min_qty) * $step));
			if(MODULE_SEO_URL_INDEX_STATUS == 'True')
				$link = href_link('tag/'.urlencode($key).'/');
			else
				$link = href_link('taglisting.php', 'tag='.urlencode($key));
			$cloud .= $h_tag[$h_rand][0].'<a href="'.$link.'" class="fs'.$size.'" title="'.$value.' Produkte wurden mit '.$key.' getagged">'.$key.'</a>'.$h_tag[$h_rand][1].' ';
	    }
	    return $cloud;
	}

	$data = $db->db_query_limit("SELECT
									pID,
									tag,
									COUNT(tag) AS tag_anzahl
								FROM
									".TABLE_TAG_TO_PRODUCT."
								WHERE
									lID = '".$_SESSION['languages_id']."'
								GROUP BY
									tag
								ORDER BY RAND()", MAX_DISPLAY_TAGS);

	if($data->_numOfRows) {
		$tag_array = array();
		while(!$data->EOF) {
			if(!empty($data)){
				$check = $db->db_query("SELECT products_id FROM ".TABLE_PRODUCTS." WHERE products_id = '".$data->fields['pID']."' AND products_status = '1'");
				if($check->_numOfRows)
					$tag_array[$data->fields['tag']] = $data->fields['tag_anzahl'];
			}
			$data->MoveNext();
		}
	}
	if(is_array($tag_array))
		$tag_cloud = printTagCloud($tag_array);

	$box_smarty->assign('html_tpl_path', CURRENT_TEMPLATE.'/html');
	$box_smarty->assign('BOX_CONTENT', $tag_cloud);
}

if(!$cache)
	$box_content = $box_smarty->fetch(CURRENT_TEMPLATE.'/boxes/box_tagcloud.html');
else
	$box_content = $box_smarty->fetch(CURRENT_TEMPLATE.'/boxes/box_tagcloud.html', $cache_id);
?>